import {
	clearDrawGeometry,
	Graphic,
	union,
	highlightByGeometryList,
	planarArea,
	intersect
} from 'lark-map'
import LayerUtils from "@/utils/yc/LayerUtils";
import SymbolUtils from "@/utils/yc/SymbolUtils";
import GeometryUtils from "@/utils/yc/GeometryUtils";
import Utils from "@/utils/yc/Utils";
import { guide } from "@/api/eco/eco";
/**
 * 生态适宜指导
 */
export default {
	data() {
		return {
			//点击图形选择指导方案
			selected: {
				baseData: {
					jd: 0,//经度
					wd: 0,//纬度
					mj: 0,//总面积
					pf: 0,//评分
				},
				//生态功能区信息
				stgnqxx: {
					baseData: {
						bh: "",//编号
						mj: 0,//面积(平方米)
						sylx: "",//适宜推荐类型
						syfz: 89.99//适宜分值
					},
					//分类分值
					flfz: [
						{ stfl: "气候气象", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 },
						{ stfl: "土壤指数", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 },
						{ stfl: "地理地貌", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 }
					]
				},
				//适宜植物信息
				syzwxx: [
					{ zwmc: "沙棘", ssks: "灌木类", stzsfz: "气候气象83.5，地理地貌78.5、土壤指数39.2", sydf: 87.88 },
					{ zwmc: "柳木", ssks: "乔木类", stzsfz: "气候气象83.5，地理地貌78.5、土壤指数39.2", sydf: 86.88 },
					{ zwmc: "胡杨", ssks: "XXX类", stzsfz: "气候气象81.5，地理地貌74.5、土壤指数49.2", sydf: 77.88 }
				],
				//图形
				features: null,
				//指导方案详细信息
				zdfa: [{
					zzlx: "乔木",
					fams: "土壤适宜度评分98.9分，气候适宜度评分76.6分，地理地貌适宜度评分59.24分",
					pf: "89.34",
				}, {
					zzlx: "乔木",
					fams: "土壤适宜度评分98.9分，气候适宜度评分76.6分，地理地貌适宜度评分59.24分",
					pf: "89.34",
				}
				]
			},


			//全部返回值
			resultObject: {
				//基础信息
				baseData: {
					xzqmc: "",//行政区名称
					sl: 0,//划片区数量
					jd: 0,//经度
					wd: 0,//纬度
					mj: 0 //总面积
				},
				//点击选择图形数据信息
				selected: null,
				//生态功能区统计分析
				tjfx: [
					{ sylx: "宜灌木区", sl: 1, mj: 234.324, syfz: 67.87 },
					{ sylx: "宜乔木区", sl: 2, mj: 234.324, syfz: 67.87 },
					{ sylx: "宜草区", sl: 3, mj: 234.324, syfz: 67.87 },
					{ sylx: "不宜生长区", sl: 4, mj: 234.324, syfz: 67.87 }
				],

				//功能区明细
				gnqmx: [
					{
						baseData: {
							bh: "行政编号+OBJECTID",//编号
							mj: 0, //总面积
							sylx: "适宜类型",//适宜类型
							syfz: 0,//适宜分值
						},
						//分类分值
						flfz: [
							{ stfl: "气候气象", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 },
							{ stfl: "土壤指数", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 },
							{ stfl: "地理地貌", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 }
						]
					},
					{
						baseData: {
							bh: "",//编号
							mj: 0, //总面积
							sylx: "适宜类型",//适宜类型
							syfz: 0,//适宜分值
						},
						//分类分值
						flfz: [
							{ stfl: "气候气象", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 },
							{ stfl: "土壤指数", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 },
							{ stfl: "地理地貌", ppfz: 45.44, jqfz: 76.55, sydf: 87.88 }
						]
					}
				],
				//图形
				features: null,
			},
		}
	},

	methods: {

		/**
			* 点击查询坡度数据
			* @param {*} view 地图
			* @param {*} drawGraphics 绘制图形
			* @param {*} point 外围图形
			*/
		async queryDataBySuitable(view, drawGraphics, point) {
			clearDrawGeometry({ view: view })
			let geometries = [];
			if (drawGraphics && drawGraphics.length > 0) {
				drawGraphics.forEach(drawGraphic => {
					if (drawGraphic) {
						let interGeometry = intersect({ geometry: drawGraphic.geometry, intersector: point });
						if (interGeometry) {
							geometries.push(interGeometry);
						}
					}
				});
			}
			if (geometries.length === 0) return null;
			let results = await guide({ geometry: JSON.stringify([point]), mapExtent: JSON.stringify(view.extent) })
			if (results === null && !results.hasOwnProperty(features)) return null;
			let features = results.features;
			if (features === null && features.length === 0) return null;
			this.selected.features = [];
			let feature = features[0];
			let pdGeometry = null;
			drawGraphics.forEach(gra => {
				let interGeometry = intersect({ geometry: feature.geometry, intersector: gra.geometry });
				if (interGeometry) {
					pdGeometry = interGeometry;
				}
			});
			if (pdGeometry === null) return null;
			feature.geometry = pdGeometry;
			let center = pdGeometry.extent.center;
			//中心点渲染
			highlightByGeometryList({ view, geometryList: [pdGeometry, center], symbol: [SymbolUtils.HIGHLIGHT_RENDER, SymbolUtils.LOCATION_PICTURE] });
			//生态功能区信息
			this.selected.stgnqxx = this.getGnqmxItem(feature);
			//适宜植物信息
			this.selected.syzwxx = this.getSyzwxx(feature);
			//指导方案信息
			this.selected.zdfa = this.getZdfaxx(feature);
			//基础数据
			let x = center.longitude.toFixed(4);
			let y = center.latitude.toFixed(4);
			let attributes = feature.attributes;
			let mj = 0;
			if (attributes.hasOwnProperty("MJ")) {
				mj = Number(attributes.MJ).toFixed(2);
			}
			let syfz = 0;
			if (feature.attributes.hasOwnProperty("syfz")) {
				syfz = feature.attributes.syfz;
			}
			this.selected.baseData = {
				jd: x === undefined ? 0 : x,//经度
				wd: y === undefined ? 0 : y,//纬度
				mj: mj,//总面积
				pf: syfz//评分
			};
			this.selected.features.push(this.getGeoJSONFeature(feature));
			this.resultObject.selected = this.selected;
			return this.resultObject;

		},


		/*
			* 根据多边形查询坡度服务数据
			* @param {*} view 地图
			* @param {*} districtFeature 行政区
			* @param {*} drawGraphic     绘制图形 
			*/
		async queryDataByPolygon(view, districtFeature, drawGraphics) {
			this.resultObject.selected = null;
			this.resultObject.tjfx = [];//生态功能区统计分析
			this.resultObject.gnqmx = [];//功能区明细
			this.resultObject.features = [];//返回features
			if (drawGraphics && drawGraphics.length > 0) {
				this.wktgeometries = [];
				let geometries = [];
				drawGraphics.forEach(drawGraphic => {
					if (drawGraphic) {
						let geometry = intersect({ geometry: districtFeature.geometry, intersector: drawGraphic.geometry });
						if (geometry) {
							let wkt = GeometryUtils.multipolygonToWkt(geometry);
							this.wktgeometries.push(wkt);
							geometries.push(geometry);
						}
					}
				});
				if (geometries.length === 0) return null;
				let geo = union(geometries);
				let dGraphic = { geometry: geo, symbol: SymbolUtils.DRAW_POLYGON_RENDER };
				LayerUtils.getRenderDrawLayer(view).add(dGraphic);
				clearDrawGeometry({ view: view });
				view.goTo(dGraphic);
				let results = await guide({ geometry: JSON.stringify(geometries), mapExtent: JSON.stringify(view.extent) })
				//生态功能区统计分析
				if (results.tjfx) {
					this.resultObject.tjfx = results.tjfx;
				}
				if (this.resultObject.hasOwnProperty("features") && results.features.length > 0) {
					return this.resultHandler(view, results.features, districtFeature, dGraphic);
				}
			}
		},


		/**
		 * 查询返回结果集处理函数
		 * @param {*} view 
		 * @param {*} features 
		 * @param {*} districtFeature 
		 * @param {*} drawGraphic 
		 */
		resultHandler(view, features, districtFeature, drawGraphic) {
			if (features === null && features.length === 0) return;
			let names = [];//行政区集合，判断名称是否重复，不重复添加到name属性种
			let zmj = 0;//总面积
			for (let index = 0; index < features.length; index++) {
				let feature = features[index];
				feature.symbol = SymbolUtils.setRenderer(feature.attributes.PDJB);
				let pdGeometry = intersect({ geometry: drawGraphic.geometry, intersector: feature.geometry });
				if (pdGeometry) {
					feature.geometry = pdGeometry;
					zmj += Number(feature.attributes.MJ);//planarArea({ geometry: feature.geometry });
					//获取行政区名称
					this.getDistrictName(feature, names);
					//功能区明细
					this.resultObject.gnqmx.push(this.getGnqmxItem(feature));
					//设置geojson数据
					this.resultObject.features.push(this.getGeoJSONFeature(feature));
				}
			}
			LayerUtils.getRenderDrawLayer(view).addMany(features, 0);
			//添加基础数据
			let center = drawGraphic.geometry.extent.center;
			let x = center.longitude.toFixed(4);
			let y = center.latitude.toFixed(4);
			this.resultObject.baseData = {
				xzqmc: districtFeature.attributes.NAME + names.join("、"),
				jd: x === undefined ? 0 : x,
				wd: y === undefined ? 0 : y,
				mj: zmj.toFixed(2),
				sl: features.length
			};
			return this.resultObject;
		},

		/**
		 * 获取jsonFeature对象
		 * @param {*} feature 
		 */
		getGeoJSONFeature(feature) {
			const code = feature.attributes.TOWN_CODE;
			const objectId = feature.attributes.OBJECTID;
			const fileName = Utils.getFileName(code, objectId);
			delete feature.attributes.flfz
			delete feature.attributes.syzw
			let geoJson = GeometryUtils.graphicToGeoJson([feature]);
			return {
				fileName: fileName,
				jsonFeature: JSON.stringify(geoJson)
			}
		},

		/**
		 * 获取功能区名称数据
		 * @param {*} feature 
		 */
		getGnqmxItem(feature) {
			let code = feature.attributes.TOWN_CODE;
			let objectId = feature.attributes.OBJECTID;
			let fileName = Utils.getFileName(code, objectId);
			let mj = Number(feature.attributes.MJ);//planarArea({ geometry: feature.geometry });
			return {
				baseData: {
					bh: fileName,//编号
					mj: mj.toFixed(2), //总面积
					sylx: feature.attributes.sylx === undefined ? "" : feature.attributes.sylx,//适宜类型
					syfz: feature.attributes.syfz === undefined ? "" : feature.attributes.syfz,//适宜分值
				},
				flfz: feature.attributes.flfz
			}
		},
		/**
		 * 获取行政区名称
		 * @param {*} feature 坡度图数据
		 * @param {*} names 	行政区名称集合，判断行政区是否重复，不重复添加到name后面
		 */
		getDistrictName(feature, names) {
			let townName = feature.attributes.TOWN_NAME;
			if (names.indexOf(townName) == -1) {
				names.push(townName);
			}
		},

		/**
		 * 适宜植物信息
		 * @param {*} feature 
		 */
		getSyzwxx(feature) {
			let syzwxx = [];
			let syzw = feature.attributes.syzw;
			if (syzw && syzw.length > 0) {
				syzw.forEach(item => {
					let sytes = item.stsyfz;
					let ms = [];
					if (sytes && sytes.length > 0) {
						sytes.forEach(syte => {
							ms.push(syte.stfl + "评分" + syte.syfz + "分");
						});
					}
					syzwxx.push({
						zwmc: item.mc,
						ssks: item.ssks,
						sydf: item.syfz,
						stzsfz: ms.join(",")
					});
				});
			}
			return syzwxx;
		},

		/**
		 * 指导方案信息
		 * @param {*} feature 
		 */
		getZdfaxx(feature) {
			let zdfas = [];
			let syzw = feature.attributes.syzw;
			if (syzw && syzw.length > 0) {
				syzw.forEach(item => {
					let sytes = item.stsyfz;
					let ms = [];
					if (sytes && sytes.length > 0) {
						sytes.forEach(syte => {
							ms.push(syte.stfl + "评分" + syte.syfz + "分");
						});
					}
					zdfas.push({
						zwmc: item.mc,
						zzlx: item.ssks,
						pf: item.syfz,
						fams: ms.join(",")
					});
				});
			}
			return zdfas;
		},
		//
	},
}